from unittest import TestCase
from ddt import ddt, data, unpack
from selenium import webdriver
import Data
import Operation
import pymysql

@ddt
class Test_Pay(TestCase):
    @data(*Data.Pay.no_paywd)
    @unpack
    def testpay(self, name, pwd, paywd, expect):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()
        pay = Operation.Pay()
        pay.pay(name, pwd, paywd, driver)
        result = pay.pay_result(driver)
        if result != expect:
            driver.save_screenshot(f'{name}{pwd}{paywd}.jpg')
        self.assertEqual(expect, result)

    @data(*Data.Pay.default1)
    @unpack
    def test_default1(self, name, pwd, paywd, expect):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi7' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"update bankcard set balance=10000 where userId={userid} and defaultl=1"
        cursor.execute(sql)
        connect.commit()

        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()
        pay = Operation.Pay()
        pay.default1(name, pwd, paywd, driver)
        result = pay.default1_result(driver)
        if result[0] != expect or result[1] != '账户余额：7000.00元':
            driver.save_screenshot(f'{name},{pwd},{paywd}.jpg')
        self.assertEqual(expect, result[0])
        self.assertEqual('账户余额：7000.00元', result[1])

    @data(*Data.Pay.default1)
    @unpack
    def test_default2(self, name, pwd, paywd, expect):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi7' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"update bankcard set balance=10000 where userId={userid} and defaultl=1"
        cursor.execute(sql)
        connect.commit()

        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()
        pay = Operation.Pay()
        pay.default2(name, pwd, paywd, driver)
        result = pay.default2_result(driver)
        if result[0] != expect or result[1] != '账户余额：7000.00元':
            driver.save_screenshot(f'{name},{pwd},{paywd}.jpg')
        self.assertEqual(expect, result[0])
        self.assertEqual('账户余额：7000.00元', result[1])
